package com.cloud.open.controller;

import com.cloud.common.bean.PageInfo;
import com.cloud.common.bean.ResultInfo;
import com.cloud.open.constant.ErrorEnum;
import com.cloud.open.dto.ResourceTypeInsertParamDTO;
import com.cloud.open.dto.ResourceTypeQueryParamDTO;
import com.cloud.open.dto.ResourceTypeUpdateParamDTO;
import com.cloud.open.entity.ResourceType;
import com.cloud.open.service.ResourceTypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * 资源分类接口
 */
@Slf4j
@RestController
public class ResourceTypeController {

    @Autowired
    private ResourceTypeService resourceTypeService;

    /**
     * 查询资源分类
     *
     * @param paramDTO
     * @return ResultInfo
     */
    @PostMapping("/open/resourceType/query")
    public ResultInfo<PageInfo<ResourceType>> query(@Valid @RequestBody ResourceTypeQueryParamDTO paramDTO) {
        PageInfo<ResourceType> pageInfo = resourceTypeService.getPage(paramDTO);
        return ResultInfo.success().setData(pageInfo);
    }

    /**
     * 获取所有资源分类
     *
     * @return ResultInfo
     */
    @PostMapping("/open/resourceType/listAll")
    public ResultInfo<List<ResourceType>> listAll() {
        List<ResourceType> list = resourceTypeService.getAll();
        return ResultInfo.success().setData(list);
    }

    /**
     * 添加资源分类
     *
     * @param paramDTO
     * @return ResultInfo
     */
    @PostMapping("/open/resourceType/insert")
    public ResultInfo<String> insert(@Valid @RequestBody ResourceTypeInsertParamDTO paramDTO) {
        if (resourceTypeService.checkNameExist(paramDTO.getName(), null)) {
            return ErrorEnum.NAME_EXIST.resultInfo();
        }

        ResourceType resourceType = new ResourceType();
        resourceType.setCreateTime(new Date());
        BeanUtils.copyProperties(paramDTO, resourceType);

        if (resourceTypeService.save(resourceType)) {
            return ResultInfo.success();
        }
        return ResultInfo.failure();
    }

    /**
     * 更新资源分类
     *
     * @param paramDTO
     * @return ResultInfo
     */
    @PostMapping("/open/resourceType/update")
    public ResultInfo<String> update(@Valid @RequestBody ResourceTypeUpdateParamDTO paramDTO) {
        if (resourceTypeService.checkNameExist(paramDTO.getName(), paramDTO.getId())) {
            return ErrorEnum.NAME_EXIST.resultInfo();
        }

        ResourceType resourceType = new ResourceType();
        BeanUtils.copyProperties(paramDTO, resourceType);

        if (resourceTypeService.updateById(resourceType)) {
            return ResultInfo.success();
        }
        return ResultInfo.failure();
    }

    /**
     * 删除资源分类
     *
     * @param id
     * @return ResultInfo
     */
    @PostMapping("/open/resourceType/delete")
    public ResultInfo<String> delete(Integer id) {
        if (id == null) {
            return ErrorEnum.ID_NOT_NULL.resultInfo();
        }
        resourceTypeService.removeById(id);
        return ResultInfo.success();
    }
}

